<?php


namespace Xy\Rbac\Providers;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider;
use Illuminate\Support\Facades\Route;
use Xy\Rbac\Middleware\After;

class RbacServiceProvider extends RouteServiceProvider {
    /**
     * 这里的类提供用用户guard的获取和用户列表query获取 实现类，你可以自定义帮助类（继承\Xy\Rbac\Utils\UserHelperInterface接口的子类），可以通过如下方式
     * e.g,
     * \Xy\Rbac\Providers\RbacServiceProvider 子类中添加如下代码
     * public function boot() {
     *     parent::$userHelperClass = \Xy\Rbac\Utils\UserHelperInterfaceSonClass;
     *     parent::boot();
     * }
     * guard关联的类要继承自 @see \Illuminate\Auth\GuardHelpers
     * 用户角色分配页面用户获取的模型获取关联的guard,根据这个来获取用户模型进行查询用户列表
     * @var null
     */
    public static $userHelperClass = \Xy\Rbac\Utils\UserHelper::class;
    
    /**
     * @var \Xy\Rbac\Utils\UserHelperInterface
     */
    protected static $_userHelper;
    
    /**
     * api前缀
     * @var string
     */
    protected $_routePrefix = '/api/admin/';
    
    /**
     * 是否使用dingo
     * @var bool
     */
    protected $_isDingo = false;
    
    /**
     * 路由文件路径
     * @var null
     */
    protected $_routePath = null;
    
    /**
     * 额外中间件
     * @var array
     */
    protected $_otherMiddlewareList = [];
    
    /**
     * dingo配置版本
     * @var string
     */
    protected $_versionOfDingo = 'v1';
    
    /**
     * 控制器命名空间,不可改变
     * @var string
     */
    private $_namespace = 'Xy\Rbac\Controllers';
    
    /**
     * @var string
     */
    protected $_module = 'rbac';
    
    /**
     * 结果处理中间件
     * @var string
     */
    public static $formAfterMiddlewareList = [After::class];
    
    public function boot() {
        parent::boot();
    }
    
    public function map() {
        $this->_isDingo ? $this->_dingoRoute() : $this->_normalRoute();
    }
    
    public function register() {
        parent::register();
    }
    
    /**
     * 获取路由文件位置
     * @return null|string
     */
    protected function _getRoutePath() {
        $routePath = dirname(__DIR__) . DIRECTORY_SEPARATOR .
                     ($this->_isDingo ? 'dingo_routes.php' : 'routes.php');
        
        return $this->_routePath ? $this->_routePath : $routePath;
    }
    
    /**
     * 获取路由前缀
     * @return string
     */
    protected function _getRoutePrefix() {
        return $this->_routePrefix . mb_strtolower($this->_module);
    }
    
    /**
     * 获取中间件
     * @return array
     */
    protected function _getMiddleware() {
        return array_merge($this->_otherMiddlewareList, static::$formAfterMiddlewareList ?: []);
    }
    
    /**
     * laravel默认路由设置
     */
    protected function _normalRoute() {
        Route::prefix($this->_getRoutePrefix())
            ->middleware($this->_getMiddleware())
            ->namespace($this->_namespace)
            ->group($this->_getRoutePath());
    }
    
    /**
     * dingo路由设置
     */
    protected function _dingoRoute() {
        $namespace  = $this->_namespace;
        $version    = $this->_versionOfDingo;
        $prefix     = $this->_getRoutePrefix();
        $middleware = $this->_getMiddleware();
        require $this->_getRoutePath();
    }
    
    /**
     * @return \Xy\Rbac\Utils\UserHelperInterface
     */
    protected static function getUserHelper() {
        if (!self::$_userHelper) {
            self::$_userHelper = new self::$userHelperClass;
        }
        
        return self::$_userHelper;
    }
    
    /**
     * 获取guard
     * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Auth\GuardHelpers
     */
    static function getAuthGuardUser() {
        return self::getUserHelper()->getAuthGuardUser();
    }
    
    /**
     * @return \Illuminate\Database\Query\Builder
     */
    static function getUserQuery() {
        return self::getUserHelper()->getUserQuery();
    }
    
    /**
     * @return int
     */
    static function getUserId() {
        return self::getUserHelper()->getUserId();
    }
}